<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\agent\controller;

class AdminAuthController extends \app\BaseController
{
    public function auth()
    {
        $input = json_decode($this->request->getInput(), true);
        $admin = \app\agent\model\AdminModel::where([["account", "=", $input["account"] ?? ""], ["deleted", "=", 0], ["status", "=", 1]])->findOrEmpty();
        if($admin->isEmpty()) {
            return $this->error("用户不存在", 400);
        }
        if(!checkPasswd($input["passwd"], $admin["offset"], $admin["passwd"])) {
            return $this->error("密码错误", 400);
        }
        $user = ["admin_id" => $admin->admin_id, "level" => $admin->level, "account" => $admin->account, "role" => $admin->role->role_name ?? "user", "role_name" => $admin->role->role_name ?? "user", "uniacid" => $admin->appAdmin->modular_id ?? -1];
        if($user["uniacid"] == -1) {
            return $this->error("用户没有绑定小程序， 请联系代理端超级管理员");
        }
        if(isset($input["isAgent"]) && $input["isAgent"] && $user["role"] != "admin") {
            return $this->error("普通用户禁止访问");
        }
        if(isset($input["isAgent"]) && !$input["isAgent"] && $user["role"] == "admin") {
            return $this->error("超级管理员禁止访问， 请创建子管理员账号并绑定小程序登录");
        }
        $result["user"] = $user;
        $result["token"] = createToken();
        if(empty($result["token"])) {
            return $this->error("系统错误", 400);
        }
        setUserForToken($result["token"], $user, 99999999);
        return $this->success($result, 200);
    }
    public function unAuth()
    {
        $header = $this->request->header();
        $token = $header["token"] ?? NULL;
        if($token == NULL || !getUserForToken($token)) {
            $this->error("用户未登录");
        }
        delUserForToken($token);
        return $this->success(true);
    }
    public function AuthStatus()
    {
        $header = $this->request->header();
        $token = $header["token"] ?? NULL;
        $user = getUserForToken($token);
        $resData = longbing_auth_status($user["uniacid"]);
        return $this->success($resData);
    }
    public function isWe7()
    {
        $is_we7 = defined("IS_WEIQIN");
        return $this->success($is_we7);
    }
    public function success($data, $code = 200)
    {
        $result["data"] = $data;
        $result["code"] = $code;
        $result["sign"] = NULL;
        if(!empty($this->_token)) {
            $result["sign"] = createSimpleSign($this->_token, is_string($data) ? $data : json_encode($data));
        }
        return $this->response($result, "json", $code);
    }
    public function error($msg, $code = 400)
    {
        $result["error"] = \think\facade\Lang::get($msg);
        $result["code"] = $code;
        return $this->response($result, "json", 200);
    }
    protected function response($data, $type = "json", $code = 200)
    {
        return \think\Response::create($data, $type)->code($code);
    }
}

?>